package com.ttc.mBackupK16.BackupPhotos;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;

import com.ttc.mBackupK16.R;



import android.content.Context;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class FileItemAdapter extends ArrayAdapter<FileItem>{
	private LayoutInflater inflater;
	private ArrayList<FileItem> fileList = new ArrayList<FileItem>();
	private Context context;
	private final int file_resource_layout;
	
	public FileItemAdapter(final Context context,int resource_layout) {
		super(context,0);
		this.file_resource_layout = resource_layout;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return fileList.size();
	}

	@Override
	public FileItem getItem(int index) {
		// TODO Auto-generated method stub
		return fileList.get(index);
	}

	@Override
	public long getItemId(int index) {
		// TODO Auto-generated method stub
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final View view = getWorkingView(convertView);
		final ViewHolder viewHolder = getViewHolder(view);
		
		FileItem item  = getItem(position);
		viewHolder.textView.setId(position);
		viewHolder.textView.setText(item.getFileName());
						
		return convertView;
	}
	private View getWorkingView(final View convertView){
		View workingView = null;
		if(convertView == null){
			final Context context = getContext();
			final LayoutInflater inflater = (LayoutInflater) context.getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			workingView = inflater.inflate(file_resource_layout, null);
		}else{
			workingView = convertView;
		}
		
		return workingView;
	}
	
	private ViewHolder getViewHolder(final View workingView){
		final Object tag = workingView.getTag();
		ViewHolder viewHolder = null;
		if(tag == null || !(tag instanceof ViewHolder)){
			viewHolder = new ViewHolder();
			viewHolder.imageView = (ImageView) workingView.findViewById(R.id.ic_icon);
			viewHolder.textView = (TextView) workingView.findViewById(R.id.tv_file_name);
		}else{
			viewHolder = (ViewHolder) tag;
		}
		return viewHolder;
	}
	private static class ViewHolder {
		ImageView imageView;
		EditText editText;
		TextView textView;
	}

	

}
